package valleyOfDemon;

import pojo.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 模型封装，Servlet之间共享模型数据，Servlet之间的信息传递
 * 在ServletDemo02第一行调用了此类的共享信息
 */
@WebServlet("/ServletDemo03")
public class ServletDemo03 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        System.out.println(req.getParameter("name"));
        System.out.println(req.getParameter("age"));
        String[] wives = req.getParameterValues("wife");
        for (String wife : wives) {
            System.out.println(wife);
        }
        User user = new User();
        user.setName(req.getParameter("name"));
        user.setAge(req.getParameter("age"));
        user.setWife(wives);
        getServletContext().setAttribute("姜衡一家子", user);
        System.out.println("姜衡过上了幸福快乐的生活");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
